<?php
// +----------------------------------------------------------------------
// | File members_controller
// +----------------------------------------------------------------------
// | 花集管理系统 Copyright (c) 2011.
// +----------------------------------------------------------------------
// | Author: daly version 1.0
// +----------------------------------------------------------------------
//
class UsersController extends AppController{
    var $name   = 'Users';
    var $layout = 'layout';
    var $uses   = array('User','Shop');
    var $components = array('Captcha');


    function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->autoRedirect = false;
        $this->Auth->allow('*');

    }
    function cakkback(){
        $this->render('cakkback');
    }
    /**
     +----------------------------------------------------------
     * @会员注册初始化函数
     +----------------------------------------------------------
     * @access
     +----------------------------------------------------------
     * @view::register
     +----------------------------------------------------------
     */
    function register(){
        $this->render('register');
    }

    /**
     +-----------------------------------------------------------
     * @会员注册
     +-----------------------------------------------------------
     * @access
     +-----------------------------------------------------------
     * @view:success
     +-----------------------------------------------------------
     */
    function doRegister(){
        $this->data['User']['pwd'] = $this->params['form']['repeat_password']; //记录密码明文
        if( $this->data ){
            $this->User->save($this->data,false);
        } else{
            $this->redirect('/users/register');
        }
        //$this->render('done_register');
        $this->redirect('/users/doneRegister');
    }

    /*
     * @会员注册完成
     * @access
     * @view:complete
     */
     function doneRegister(){
         $this->render('done_register');
     }

    /**
     +----------------------------------------------------------
     * @检查用户名是否已经存在
     +----------------------------------------------------------
     * @access
     +----------------------------------------------------------
     * @return true or false;
     +----------------------------------------------------------
     */
    function checkUsername(){
        $username = $_POST['UserUsername'];
        //var_dump($username);die;
        $params = array();
        $params['conditions'] = array('username'=>$username);
        $count = $this->User->find('count',$params);

        if( $count==1 ){
            echo false;
        } else{
            echo true;
        }
        exit();
    }


    /**
     * @登录
     * @access
     * @view:login
     */
    function login(){
        $this->layout = '';
        if ($this->Auth->user()) {           
            //1.设置COOKIE
            if( isset($this->params['data']['User']['status']) ){
                $status = $this->params['data']['User']['status'];
                if( $status==0 ){
                    $cookie_lifetime = 0;
                } else if( $status==1 ){
                    $cookie_lifetime = "+1 weeks";
                } else if( $status==2 ){
                    $cookie_lifetime = "+1 months";
                } else if( $status==3 ){
                    $cookie_lifetime = "+3 months";
                }
                $cookie = array();
                $cookie['username'] = $this->data['User']['username'];
                $cookie['password'] = $this->data['User']['password'];
                $this->Cookie->write('Auth.User', $cookie, true,$cookie_lifetime);
         
                //2.记录登录时间
                $userInfo        = $this->Auth->user();
                $this->User->id = $userInfo['User']['id'];
                $lastdate        = strtotime(date("Y-m-d h:i:s"));
                $this->User->saveField("lastdate",$lastdate);
            }
            //2.跳转到后台首页
            $this->redirect('/ht/main');
        }
        //3.从cookie中读取
        if (empty($this->data)) {
            $cookie = $this->Cookie->read('Auth.User');
            if (!is_null($cookie)) {
                if ($this->Auth->login($cookie)) {
                    $this->Session->delete('Message.auth');
                    $this->redirect('/ht/main');
                }
            }
        }
        
    }

    function logout(){
        $this->Cookie->delete('Auth.User');
        $this->redirect($this->Auth->logout());
    }


    /**
     * @找回密码
     * @access
     * @view:getpassword
     */
    function getPassword(){
        $this->layout = '';
        $this->render('getpassword');
    }

    function sedPassword(){
        $mobile = $_POST['mobile'];
        var_dump($result);die;

    }

    function captcha() {
        //Configure::write('debug', '0');
        $this->autoRender = false;
        $this->Captcha->render();
    }

    /*
     * 注册花店初始页面
     */
    function floristapply(){
//        var_dump($this->Auth->User());die;
        $user = $this->Auth->User();
        $this->data['User']['mobilephone'] = $user['User']['mobilephone'];
        $this->render();
    }

    /*
     * 注册花店
     */
    function doFloristapply(){
        $user = $this->Auth->User();
        
        $this->data['Shop']['user_id'] = $user['User']['id'];
        //$this->data['Shop']['shopname'] = "店";
        $this->data['Shop']['province'] = "店";
        $this->data['Shop']['city'] = "店";
        $this->data['Shop']['area'] = "店";
        $this->Shop->save($this->data,false);

        $this->Auth->login($this->data);
        $this->redirect("/ht/main");
    }
}


?>